﻿using Aspose.Cells;
using CCTool.Scripts.Manager;
using CCTool.Scripts.ToolManagers.Extensions;
using CCTool.Scripts.ToolManagers.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CCTool.Scripts.DataPross.Excel
{
    /// <summary>
    /// Interaction logic for ExcelToDict.xaml
    /// </summary>
    public partial class ExcelToDict : ArcGIS.Desktop.Framework.Controls.ProWindow
    {
        // 工具设置标签
        readonly string toolSet = "ExcelToDict";

        public ExcelToDict()
        {
            InitializeComponent();

            // 初始化其它参数选项
            textExcelPath.Text = BaseTool.ReadValueFromReg(toolSet, "excelPath");
            txt_sheetIndex.Text = BaseTool.ReadValueFromReg(toolSet, "sheetIndex", "1");
            txt_startRow.Text = BaseTool.ReadValueFromReg(toolSet, "startRow", "2");
            txt_keyCol.Text = BaseTool.ReadValueFromReg(toolSet, "keyCol", "1");
            txt_valueCol.Text = BaseTool.ReadValueFromReg(toolSet, "valueCol", "2");

            rb_all.IsChecked = BaseTool.ReadValueFromReg(toolSet, "isAll", "true").ToBool();
            rb_key.IsChecked = BaseTool.ReadValueFromReg(toolSet, "isKey", "false").ToBool();
            rb_value.IsChecked = BaseTool.ReadValueFromReg(toolSet, "isValue", "false").ToBool();


        }

        // 定义一个进度框
        private ProcessWindow processwindow = null;
        string tool_name = "从Excel表中提取字典";

        private void openExcelButton_Click(object sender, RoutedEventArgs e)
        {
            textExcelPath.Text = UITool.OpenDialogExcel();
        }


        private void btn_go_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // 获取指标
                string excelPath = textExcelPath.Text;
                int sheetIndex = txt_sheetIndex.Text.ToInt();
                int startRow = txt_startRow.Text.ToInt();
                int keyCol = txt_keyCol.Text.ToInt();
                int valueCol = txt_valueCol.Text.ToInt();

                bool isAll = rb_all.IsChecked.Value;
                bool isKey = rb_key.IsChecked.Value;
                bool isValue = rb_value.IsChecked.Value;


                // 判断参数是否选择完全
                if (excelPath == "" || sheetIndex == 0 || startRow == 0 || keyCol == 0 || valueCol == 0)
                {
                    MessageBox.Show("有必选参数为空，或参数为0。");
                    return;
                }

                // 写入本地
                BaseTool.WriteValueToReg(toolSet, "excelPath", excelPath);
                BaseTool.WriteValueToReg(toolSet, "sheetIndex", sheetIndex);
                BaseTool.WriteValueToReg(toolSet, "startRow", startRow);
                BaseTool.WriteValueToReg(toolSet, "keyCol", keyCol);
                BaseTool.WriteValueToReg(toolSet, "valueCol", valueCol);

                BaseTool.WriteValueToReg(toolSet, "isAll", isAll);
                BaseTool.WriteValueToReg(toolSet, "isKey", isKey);
                BaseTool.WriteValueToReg(toolSet, "isValue", isValue);

                // 打开进度框
                ProcessWindow pw = UITool.OpenProcessWindow(processwindow, tool_name);
                pw.AddMessageTitle(tool_name);

                Close();

                pw.AddMessageStart("生成结果");

                string all = "";
                string keyStr = "";
                string valueStr = "";

                // 获取工作薄、工作表
                // 打开工作薄
                Workbook wb = ExcelTool.OpenWorkbook(excelPath);
                // 打开工作表
                Worksheet sheet = wb.Worksheets[sheetIndex - 1];
                Cells cells = sheet.Cells;
                for (int i = startRow - 1; i <= cells.MaxDataRow; i++)
                {
                    string key = cells[i, keyCol - 1].StringValue;
                    string value = cells[i, valueCol - 1].StringValue;

                    all += "{\"" + key + "\", \"" + value + "\"}, ";
                    keyStr += "\"" + key + "\", ";
                    valueStr += "\"" + value + "\", ";
                }

                all = all[..^2] + "\r";
                keyStr = keyStr[..^2] + "\r";
                valueStr = valueStr[..^2] + "\r";

                if (isAll)
                {
                    pw.AddMessageMiddle(80, all, Brushes.Gray);
                }
                else if(isKey)
                {
                    pw.AddMessageMiddle(80, keyStr, Brushes.Gray);
                }
                else if (isValue)
                {
                    pw.AddMessageMiddle(80, valueStr, Brushes.Gray);
                }

                wb.Dispose();

                pw.AddMessageEnd();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + ee.StackTrace);
                return;
            }
        }

        private void btn_help_Click(object sender, RoutedEventArgs e)
        {
            string url = "https://blog.csdn.net/xcc34452366/article/details/152324236";
            UITool.Link2Web(url);
        }
    }
}
